package com.ln.lnzw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.ln.lnzw.R;
import com.ln.lnzw.activity.LegalPersonRegistrationActivity;
import com.ln.lnzw.base.BaseFragment;
import com.ln.lnzw.utils.ToastFactory;
import com.ln.lnzw.view.ClearableEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by wuwei on 2018/4/3.
 * 法人注册
 */

public class LegalPersonRegistrationFragment extends BaseFragment {

    @BindView(R.id.rl_tijaio)
    RelativeLayout rlTijaio;
    Unbinder unbinder;
    @BindView(R.id.cet_jigoumingcheng)
    ClearableEditText cetJigoumingcheng;
    @BindView(R.id.cet_zhengjianhaoma)
    ClearableEditText cetZhengjianhaoma;
    @BindView(R.id.cet_fadingdaibiao)
    ClearableEditText cetFadingdaibiao;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.legal_person_registration_activity, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        cetJigoumingcheng.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYesAndNo();
            }
        });
        cetZhengjianhaoma.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYesAndNo();
            }
        });
        cetFadingdaibiao.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYesAndNo();
            }
        });


    }

    private void editTextIsYesAndNo() {
        if (!TextUtils.isEmpty(cetJigoumingcheng.getText())&&!TextUtils.isEmpty(cetZhengjianhaoma.getText())&&!TextUtils.isEmpty(cetFadingdaibiao.getText())){
            rlTijaio.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
        }
        if (TextUtils.isEmpty(cetJigoumingcheng.getText())||TextUtils.isEmpty(cetZhengjianhaoma.getText())||TextUtils.isEmpty(cetFadingdaibiao.getText())){
            rlTijaio.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.rl_tijaio)
    public void onViewClicked() {
        if (TextUtils.isEmpty(cetJigoumingcheng.getText())||TextUtils.isEmpty(cetZhengjianhaoma.getText())||TextUtils.isEmpty(cetFadingdaibiao.getText())){
            return;
        }

        if (!RegexUtils.isIDCard18(cetZhengjianhaoma.getText())){
            ToastFactory.getToast(activity,"身份证格式不正确").show();
            return;
        }
        ActivityUtils.startActivity(activity, LegalPersonRegistrationActivity.class);

    }
}
